package com.example.drive_safe;

import android.content.Context;
import android.content.SharedPreferences;

public class ApplicationSetting {
	private static int thresholdSpeed = 5;
	private static final String PREF_NAME = "drive_safe";
	
	private static final String APPLICATION_STATE = "on_off";
	private static final String CURRENT_SPEED = "speed";
	
	// Begin application status
	public static int getApplicationStatus(Context context) {
		return getInt(context, ApplicationSetting.APPLICATION_STATE);
	}
	
	//Set application status
	public static void setApplicationStatus(Context context, int status) {
		// 0 for off, 1 for on
		putInt(context, ApplicationSetting.APPLICATION_STATE, status);
	}
		
	// Set threshold speed
	public static void setThresholdSpeed(int speed) {
		thresholdSpeed = speed;
	}

	// Get threshold Speed
	public static int getThresholdSpeed() {		
		return thresholdSpeed;
	}
	
	// Get current speed
	public static int getCurrentSpeed(Context context) {
		return getInt(context, ApplicationSetting.CURRENT_SPEED);
	}
	
	// Save the current speed
	public static void setCurrentSpeed(Context context, int speed) {
		putInt(context, ApplicationSetting.CURRENT_SPEED, speed);
	}
	
	// Read data from sharedPreferencs
	private static int getInt(Context context, String tag) {
		SharedPreferences pref = context.getSharedPreferences(PREF_NAME, 0);
		
		return pref.getInt(tag, 0);
	}

	// Save data in SharedPreferences
	private static void putInt(Context context, String tag, int value) {
		SharedPreferences pref = context.getSharedPreferences(PREF_NAME, 0);
		SharedPreferences.Editor editor = pref.edit();

		editor.putInt(tag, value);
		editor.commit();
	}
	
}
